using System;
using SecuBat.Work;
using SecuBat.Threading;

namespace SecuBat.Threading
{
	/// <summary>
	/// Summary description for WorkerThread.
	/// </summary>
	public class WorkerThread : ThreadBase 
	{
		private Task _workingTask;

		public Task WorkingTask
		{
			get { return _workingTask; }
			set { _workingTask = value; }
		}

		public WorkerThread(Task task) 
		{
			_workingTask = task;
            _workingTask.SetRunningInWorkerThread(this);
		}

        public void SignalizeStatusChange()
        {
            FireStatusTick(this, StatusTickArgs.States.Running);
        }

		public override int Run() 
		{
			try 
			{
				FireStatusTick(this, StatusTickArgs.States.Running );
				if (this.WorkingTask != null)
				{
					this.WorkingTask.Run();
				}
			
				FireStatusTick(this, StatusTickArgs.States.Stopped, StatusTickArgs.NormalExit );
			}
			catch( StopRequestException /*x*/ ) 
			{
				//data.AddReport( "splat - incomplete\r\n" );

				/* Don't fire events here. Because the events
								are placed into queues, doing so would likely produce 
								an undesired effect.  Stop would be requested, and
								acknowledged, then one more message would be received. 
								Only set final data and cleanup. */
				throw;
			}
			return StatusTickArgs.NormalExit;
		}
	}
}
